package com.navigation;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

import com.kogi.patterns.R;
import com.navigation.segmented.EnergyEntry;
import com.navigation.segmented.EnergyEntryHydroComparator;
import com.navigation.segmented.EnergyNuclearComparator;
import com.navigation.segmented.EnergyTotalComparator;

import android.app.Activity;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


public class SegmentedControl extends Activity{

	ArrayList<EnergyEntry> entries;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        entries = new ArrayList<EnergyEntry>();
        setContentView(R.layout.segmented_control);
        setTitle("Nicolas");
        
        final TextView val1 = (TextView)findViewById(R.id.textval1);
        final TextView val2 = (TextView)findViewById(R.id.textval2);
        final TextView val3 = (TextView)findViewById(R.id.textval3);
        final TextView val4 = (TextView)findViewById(R.id.textval4);
        final TextView val5 = (TextView)findViewById(R.id.textval5);
        final TextView val6 = (TextView)findViewById(R.id.textval6);
        final TextView val7 = (TextView)findViewById(R.id.textval7);
        final TextView val8 = (TextView)findViewById(R.id.textval8);
        final TextView val9 = (TextView)findViewById(R.id.textval9);
        final TextView val10 = (TextView)findViewById(R.id.textval10);
        
        SegmentedControlButton b1 = (SegmentedControlButton)findViewById(R.id.option1);
        SegmentedControlButton b2 = (SegmentedControlButton)findViewById(R.id.option2);
        SegmentedControlButton b3 = (SegmentedControlButton)findViewById(R.id.option3);
        
        final ArrayList<String> pais = new ArrayList<String>();
        final ArrayList<String> total = new ArrayList<String>();
        final ArrayList<String> hydro = new ArrayList<String>();
        final ArrayList<String> nuclear = new ArrayList<String>();
        
        
        
        InputStream in;
	  	
    	ContextWrapper context=this;
		in= context.getResources().openRawResource(R.raw.energy);
    	
    	
		try {
			
			 	StringBuffer inLine = new StringBuffer();
    	        InputStreamReader isr = new InputStreamReader(in);
    	 
    	        BufferedReader inRd = new BufferedReader(isr);
    	        
    	 //obtener datos
    	        String text;
    	        String titulo1,titulo2,titulo3;
    	       
    	       
    	        
    	        int fi=0;
    	        while ((text = inRd.readLine()) != null) {
    	            if(fi==0){
    	            	String titulos[] = text.toString().split(",");
    	            	titulo1=titulos[1];
    	            	titulo2=titulos[2];
    	            	titulo3=titulos[3];
    	            	setTitle(titulos[0]);
    	            	fi=1;
    	            }
    	            else{
    	            	String val[] = text.toString().split(",");
    	            	pais.add(val[0]);
    	            	total.add(val[1]);
    	            	hydro.add(val[2]);
    	            	nuclear.add(val[3]);
    	            	
    	            	entries.add( EnergyEntry.getFromLine(text) );
    	            }
    	            
    	            
    	        }
    	        in.close();
    	        
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		
		ArrayList<String> poner = new ArrayList<String>();
		poner=ordenarTotal(pais,total);
		
		val1.setText(poner.get(0));
		val2.setText(poner.get(1));
		val3.setText(poner.get(2));
		val4.setText(poner.get(3));
		val5.setText(poner.get(4));
		val6.setText(poner.get(5));
		val7.setText(poner.get(6));
		val8.setText(poner.get(7));
		val9.setText(poner.get(8));
		val10.setText(poner.get(9));
		
		//colocar los valores
		 b1.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
				   
					ArrayList<String> poner = new ArrayList<String>();
					poner=ordenarTotal(pais,total);
					
					val1.setText(poner.get(0));
					val2.setText(poner.get(1));
					val3.setText(poner.get(2));
					val4.setText(poner.get(3));
					val5.setText(poner.get(4));
					val6.setText(poner.get(5));
					val7.setText(poner.get(6));
					val8.setText(poner.get(7));
					val9.setText(poner.get(8));
					val10.setText(poner.get(9));
					
				}
				
				 

	        });
	        b2.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
				  
					ArrayList<String> poner = new ArrayList<String>();
					poner=ordenarHydro(pais,hydro);
					
					val1.setText(poner.get(0));
					val2.setText(poner.get(1));
					val3.setText(poner.get(2));
					val4.setText(poner.get(3));
					val5.setText(poner.get(4));
					val6.setText(poner.get(5));
					val7.setText(poner.get(6));
					val8.setText(poner.get(7));
					val9.setText(poner.get(8));
					val10.setText(poner.get(9));
					
				}
				
				 

	        });
	        b3.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
				   
					ArrayList<String> poner = new ArrayList<String>();
					poner=ordenarNuck(pais,nuclear);
					
					val1.setText(poner.get(0));
					val2.setText(poner.get(1));
					val3.setText(poner.get(2));
					val4.setText(poner.get(3));
					val5.setText(poner.get(4));
					val6.setText(poner.get(5));
					val7.setText(poner.get(6));
					val8.setText(poner.get(7));
					val9.setText(poner.get(8));
					val10.setText(poner.get(9));
					
				}
				
				 

	        });
		

	}
	
	public ArrayList<String> ordenarTotal(ArrayList<String> Apais,ArrayList<String> Atotal){
		
		ArrayList<String> datos=new ArrayList<String>();
		
		Collections.sort(entries, new EnergyTotalComparator());
		
		int i = 1;
		for (EnergyEntry entry : entries) {
			datos.add(String.format("%d. %s %d" , i, entry.country, entry.total));
			i++;
		}
		
		return datos;
		
		//pasardatos a int para organizarlos
		/*long mayor=0;
		int pos=0;
		int count=1;
		int tam=Atotal.size();
		for(int i=0; i<tam-1; i++){
			mayor=0;//
			pos=0;
			for(int j=0; j<tam-1; j++){
				if(mayor<Long.parseLong(Atotal.get(j).toString())){
					mayor=Long.parseLong(Atotal.get(j).toString());
					pos=j;
				}
			}
			
			datos.add(String.valueOf(count)+". "+Apais.get(pos)+"     "+String.valueOf(mayor));
			Atotal.remove(pos);
			Apais.remove(pos);
			tam=Atotal.size();
			if(count==10){
				i=tam;
			}
			count++;
			
		}
		*/
		
		
	}
public ArrayList<String> ordenarHydro(ArrayList<String> pais,ArrayList<String> total){
		
	
		ArrayList<String> result=new ArrayList<String>();
		
		
		
	/*	
		//pasardatos a int para organizarlos
		long mayor=0;
		int pos=0;
		int count=1;
		int tam=total.size();
		for(int i=0; i<tam-1; i++){
			mayor=0;//
			pos=0;
			for(int j=0; j<tam-1; j++){
				if(mayor<Long.parseLong(total.get(j).toString())){
					mayor=Long.parseLong(total.get(j).toString());
					pos=j;
				}
			}
			String con=String.valueOf(count);
			String pai=pais.get(pos).toString();
			String va=String.valueOf(mayor)	;
			
			datos.add(con+". "+pai+"     "+va);
			total.remove(pos);
			pais.remove(pos);
			tam=total.size();
			if(count==10){
				i=tam;
			}
			count++;
			
		}
//		*/
		
		Collections.sort(entries, new EnergyEntryHydroComparator());
		
		int i = 1;
		for (EnergyEntry entry : entries) {
			result.add(String.format("%d. %s %d" , i, entry.country, entry.hydro));
			i++;
		}
		
		return result;
	}
public ArrayList<String> ordenarNuck(ArrayList<String> pais,ArrayList<String> total){
	
	ArrayList<String> datos=new ArrayList<String>();
	
	Collections.sort(entries, new EnergyNuclearComparator());
	
	int i = 1;
	for (EnergyEntry entry : entries) {
		datos.add(String.format("%d. %s %d" , i, entry.country, entry.nuclear));
		i++;
	}
	
	return datos;
}
}
